<template>
  <div>
    <!--头部行表单 ：搜索 导出 -->
    <div class="filter-container">
      <el-input v-model="query.keyword" placeholder="搜索用户" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleSearch"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button :loading="exportLoading" class="filter-item" type="primary" icon="el-icon-download"
                 @click="handleExport">导出
      </el-button>
    </div>

    <!-- 数据列表  删除 -->
    <div class="list-container">
      <el-table :data="dataList" fit highlight-current-row style="width: 100%;">
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="addressName" label="收货人"></el-table-column>
        <el-table-column prop="addressTel" label="电话"></el-table-column>
        <el-table-column prop="addressDetail" label="地址"></el-table-column>
        <el-table-column prop="status" label="状态"></el-table-column>
        <el-table-column prop="createTime" label="下单时间"></el-table-column>
        <el-table-column prop="note" label="备注"></el-table-column>

        <el-table-column label="操作" width="80" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-delete" @click="handleDetail(scope.row.id)">详单</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="totalCount"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
    <!-- 详情弹出框 -->
    <order-detail v-if="detailDialogVisible" ref="detailDialog"></order-detail>
  </div>
</template>

<script>
import * as OrderAPI from '@/api/order.js';
import OrderDetail from './components/OrderDetail.vue';

export default {
  name: 'orderList',
  components: {
    OrderDetail  //注册组件
  },
  data() {
    return {
      dataList: [], //列表数据
      query: {
        keyword: '',
        pageIndex: 1,
        pageSize: 10,
        orderBy: 'id'
      },
      totalCount: 0,
      exportLoading: false, //导出 还没有实现 ，需要继续

      detailDialogVisible: false //详情框是否显示
    };
  },

  created() {
    // 获取数据
    this.loadDataList();
  },

  methods: {
    // 获取列表数据
    loadDataList() {
      OrderAPI.queryPage(this.query).then(result => {
        this.dataList = result.data.list; //返回数据结构参见API文档
        this.totalCount = result.data.totalCount;
      });
    },

    // 搜索按钮
    handleSearch() {
      this.query.pageIndex = 1;
      this.loadDataList();
    },

    // 触发导出按钮
    handleExport() {
      this.$message('导出，待完成...');
    },

    // 详单操作
    handleDetail(id) {
      this.detailDialogVisible = true; //详情框显示
      this.$nextTick(() => {
        this.$refs.detailDialog.init(id);
      });
    },

    // 分页导航=======================================
    handlePageChange(val) {
      this.query.pageIndex = val;
      this.loadDataList();
    }
  }
};
</script>

<style scoped></style>
